// pages/Earnings/Integral/Integral.js
//获取应用实例
const app = getApp();
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { getUserAccount , getUser } from '../../../api/user';
import { getOrderFormSum } from '../../../api/earning';
import { getBrandConfig , checkInCoin , queryUserIsCheck } from '../../../api/brand';
import{ queryGoodsFreightConfig } from '../../../api/goods';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareTitle: app.globalData.global.shareTitle,
    assetsAddress: assetsAddress,
    totalSum: 0, // 累计积分
    lastMonthSum: 0, // 上月积分
    integral: 0, // 当前积分
    hasBindAddress: false,
    hasBindPhone: false,
    checkInCoinNumber: 0, // 签到获取积分
    sonUserCoinAward: 0, // 推广好友注册获得的积分
    buyShopCoinAward: 10, // 商城下单奖励积分比例
    addShopNameCoinAward: 0, // 填写花店名称奖励积分
    bindPhoneCoinAward: 0, // 绑定手机号奖励积分
    standIntegral: 0, // 达标积分，免次月物流费
    isCheck: false, // 是否签到
    storehouseId: wx.getStorageSync('storehouseId'),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getUserAccount();
    this._getOrderFormSum();
    this._getBrandConfig();
    this._queryGoodsFreightConfig();
    this._queryUserIsCheck();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this._getUser();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this._getUserAccount();
    this._getOrderFormSum();
    setTimeout(()=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res,'刷新成功')
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = wx.getStorageSync('userId')
    return {
      title: this.data.shareTitle,
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 页面跳转
  goNext(e){
    let type = e.target.dataset.type||e.currentTarget.dataset.type;
    let url = e.target.dataset.name||e.currentTarget.dataset.name;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    // console.log(JSON.stringify(item))
    // return
    if(item){
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}?item=${JSON.stringify(item)}`,
        })
      }
    }else{
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}`,
        })
      }
    }
  },
  // 获取当前积分
  _getUserAccount(){
    let _this = this;
    getUserAccount('5')
    .then(res => {
      if(res.success){
        _this.setData({
          integral: res.data.balance,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none'
        })
      }
    })
  },
  // 获取累计统计
  _getOrderFormSum(){
    let _this = this;
    getOrderFormSum({accountType: '5'})
    .then(res => {
      if(res.success){
        _this.setData({
          totalSum: res.data.totalSum,
          lastMonthSum: res.data.lastMonthSum,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 获取用户信息
  _getUser(){
    let _this = this;
    getUser()
    .then(res => {
      if(res.success){
        _this.setData({
          hasBindAddress: res.data.province ? true : false,
          hasBindPhone: res.data.phone.length == 11 ? true : false,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 获取积分获取信息
  _getBrandConfig(){
    let _this = this;
    getBrandConfig({})
    .then(res => {
      if(res.success){
        console.log(res,'res')
        _this.setData({
          checkInCoinNumber: res.data[0].checkInCoinNumber,
          sonUserCoinAward: res.data[0].sonUserCoinAward,
          buyShopCoinAward: res.data[0].buyShopCoinAward * 100,
          addShopNameCoinAward: res.data[0].addShopNameCoinAward,
          bindPhoneCoinAward: res.data[0].bindPhoneCoinAward,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 去分享
  toShare(){
    wx.navigateTo({
      url: '/pages/Systems/Shares/Shares',
    })
  },
  // 去购物
  goBuy(){
    wx.switchTab({
      url: '/pages/Tabbar/Classification/Classification'
    })
  },
  // 去填写地址
  goBindAddress(){
    if(this.data.hasBindAddress){
      return;
    }
    wx.navigateTo({
      url: '/pages/Users/MyAddress/MyAddress',
    })
  },
  // 绑定手机号
  goBindPhone(){
    if(this.data.hasBindPhone){
      return;
    }
    wx.navigateTo({
      url: '/pages/Users/BindPhone/BindPhone',
    })
  },
  // 签到
  _checkInCoin(){
    let _this = this;
    checkInCoin()
    .then(res => {
      if(res.success){
        _this._getUserAccount();
        _this._getOrderFormSum();
        _this._getBrandConfig();
        _this._queryUserIsCheck();
        wx.showToast({
          title: '签到成功',
          icon: 'success',
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 获取商品运费配置
  _queryGoodsFreightConfig(){
    let _this = this;
    queryGoodsFreightConfig({storehouseId: _this.data.storehouseId})
    .then(res => {
      if(res.success){
        var integralStandard = 0;
        if(res.data.content.length > 0){
          integralStandard = res.data.content[0].integralStandard;
        }
        _this.setData({
          standIntegral: integralStandard,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 查询用户今日是否签到
  _queryUserIsCheck(){
    let _this = this;
    queryUserIsCheck()
    .then(res => {
      if(res.success){
        if(res.data == 1){
          _this.setData({
            isCheck: true,
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
})